#include "soc.h"
#include "stdlix.h"


int send_to_node(NodMessage* message)
{
    int mysocket/*,len,node_id*/;
//    int i=0;
    struct sockaddr_in6 addr;
    int addr_len;
    char msg[32];
//    char REMOTEIP[64];
//    strcpy(REMOTEIP,table[message->data[0]-1]);
//    strcpy(msg,(char*)message);
 
    if((mysocket=socket(AF_INET6,SOCK_DGRAM,0))<0)
    {
//        perror("error:");
        return 0;
    }
/*    else
    {
        printf("socket created ...\n");
        printf("socket id :%d \n",mysocket);
        printf("rmote ip : %s\n",REMOTEIP);
        printf("remote port :%d \n",REMOTEPORT);
    }*/
    addr_len=sizeof(struct sockaddr_in6);
    bzero(&addr,sizeof(addr));
    addr.sin6_family=AF_INET6;
    addr.sin6_port=htons(3002);
    //inet_pton(AF_INET6,REMOTEIP,&addr.sin6_addr);
    addr.sin6_addr=table[message->data[0]-1];
    //printf(" %d",(char)addr.sin6_addr);
/*    inet_ntop(AF_INET6,&addr.sin6_addr,REMOTEIP,sizeof(REMOTEIP));
    printf("%s\n",REMOTEIP);
    */
 
    
        //bzero(msg,sizeof(msg));
        //len=read(STDIN_FILENO,msg,sizeof(msg));//从标准输入输出中读取数据
        //msg[len-1]='\0';
        //printf("%d",len);
        //fflush(stdout);
    msg[0]=message->order;
    msg[1]=message->data[0];
    msg[2]=message->data[1];
    msg[3]=message->data[2];
    msg[4]=message->data[3];
    msg[5]=send_seq[message->data[0]-1];
    msg[6]='\0';
//    printf("*****msg %d,%d*****\n",msg[0],msg[1]);
/*    for(i=0;i<=2;i++)
    {*/
    	/*int length=*/sendto(mysocket,msg,sizeof(msg),0,(struct sockaddr *)&addr,addr_len);

//        printf("len=%d,socket=%d\n",length,mysocket);
/*        if(i==2)
            break;
        sleep(1);
    }*/
    send_seq[message->data[0]-1]++;
    if(send_seq[message->data[0]-1]==16)
        send_seq[message->data[0]-1]=0;
        //printf("send success!\n");
        
        //len=recvfrom(mysocket,msg,sizeof(msg),0,(struct sockaddr *)&addr,&addr_len);
        //printf("%d:",i);
        //i++;
        //printf("Received message : %s\n",msg);
    close(mysocket);
    return 1;

}


